package common.net
{
    import common.display.ActionImage;

    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.system.LoaderContext;
    import flash.utils.ByteArray;

    import kernel.debug.Debug;

    import org.granite.net.DataLoader;
    import org.granite.net.DataLoaderType;

    public class ActionImageLoader extends DataLoader
    {
        private var _result:int = -1;
        private var _actionImage:ActionImage;
        private var tagercount:int;

        public function ActionImageLoader()
        {
            super (DataLoaderType.BINARY);
        }

        public function createActionImage():ActionImage
        {

            return _actionImage;

        }

        protected function _loadError():void
        {
        }

        //handlers
        protected override function _loadCompleteHandler(e:Event):void
        {
            var temp:ByteArray = _loader.data;
            var lc:LoaderContext = _context;
            //stop();
            _context = lc;

            if (temp.readUnsignedInt () != 0x89504e47)
            {
                temp.position = 0;
                var len:uint = temp.readUnsignedShort ();
                _actionImage = ActionImage.createSetFromByte (temp);
                if (_actionImage)
                {
                    var count:int = _actionImage.frameCount * _actionImage.directionCount;
                    tagercount = count;
                    for (var i:int = 0 ; i < count ; i++)
                    {
                        var _imageLoader:ActionDataLoader = new ActionDataLoader ();
                        _imageLoader.addEventListener (IOErrorEvent.IO_ERROR , _imageLoadErrorHandler , false , 0 , true);
                        _imageLoader.addEventListener (Event.COMPLETE , _imageLoadCompleteHandler , false , 0 , true);
                        _imageLoader.loadActionImageData (temp , len);
                    }
                    return;
                }
            }
            _loadIOErrorHandler (new IOErrorEvent (IOErrorEvent.IO_ERROR , false , false , "ActionImageLoader decode Error"));
        }

        override protected function _loadIOErrorHandler(e:Event):void
        {
            _loadError ();
            Debug.error ("ActionImageLoader IOError:" , e);
            super._loadIOErrorHandler (e);
        }

        protected function _imageLoadCompleteHandler(e:Event):void
        {
            tagercount--;
            var _imageLoader:ActionDataLoader = e.currentTarget as ActionDataLoader;
            _imageLoader.removeEventListener (IOErrorEvent.IO_ERROR , _imageLoadErrorHandler);
            _imageLoader.removeEventListener (Event.COMPLETE , _imageLoadCompleteHandler);
            _imageLoader.close ();
            _actionImage.setActionImage (_imageLoader.actionImageData);
            if (tagercount == 0)
            {
                dispatchEvent (new Event (Event.COMPLETE));
            }
        }

        protected function _imageLoadErrorHandler(e:IOErrorEvent):void
        {
            tagercount--
            var _imageLoader:ActionDataLoader = e.currentTarget as ActionDataLoader;
            _imageLoader.removeEventListener (IOErrorEvent.IO_ERROR , _imageLoadErrorHandler);
            _imageLoader.removeEventListener (Event.COMPLETE , _imageLoadCompleteHandler);

            if (tagercount == 0)
            {
                dispatchEvent (new Event (Event.COMPLETE));
            }
        }
    }
}
